<template>
  <el-card class="box-card">
    <div>
      <h3 align="center">发票申请</h3>
      <h3 style="color: red" align="center">开票总金额{{zje}}</h3>
      <el-form :ref="rulesForm" :rules="rulesForm" :model="form" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="11">
            <el-form-item label="发票编号" prop="code">
              <el-input v-model="form.code" readonly></el-input>
            </el-form-item>
            <el-form-item label="发票抬头" prop="invoiceTitle">
              <el-input v-model="form.invoiceTitle" readonly></el-input>
            </el-form-item>
            <el-form-item label="开票金额" prop="amount">
              <el-input v-model="form.amount"></el-input>
            </el-form-item>
            <el-form-item label="税号" prop="invoiceNum">
              <el-input v-model="form.invoiceNum" readonly></el-input>
            </el-form-item>
            <el-form-item label="税率%" prop="taxRate">
              <el-input v-model="form.taxRate" readonly></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="收件人" prop="receiver">
              <el-input v-model="form.receiver"></el-input>
            </el-form-item>
            <el-form-item label="发票类型" prop="invoiceType">
              <el-select v-model="form.invoiceType" placeholder="请选择活动区域" >
                <el-option label="增值税普通发票" :value="0"></el-option>
                <el-option label="增值税专用发票" :value="1"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="收货地址" prop="revAddress">
              <el-input v-model="form.revAddress"></el-input>
            </el-form-item>
            <el-form-item label="联系电话" prop="tel">
              <el-input v-model="form.tel"></el-input>
            </el-form-item>
            <el-form-item label="发票内容">
              <el-input type="textarea" v-model="form.content"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <div align="center">
          <el-row :gutter="20">
            <el-button type="success" @click="saveInvoice(rulesForm)">保存</el-button>
            <el-button type="success">取消</el-button>
          </el-row>
        </div>
      </el-form>
    </div>
  </el-card>
</template>

<script>
  export default {
    name: "Addpay",
    data() {
      return {
        nowCustomer: {},
        form:{
          code:null,
          customerId:null,
          invoiceTitle:null,
          invoiceType:null,
          invoiceNum:null,
          taxRate:null,
          createUser:null,
          receiver:null,
          tel:null,
          revAddress:null
        },
        zje:0,
        rulesForm:{
          amount:[
            { required: true, message: '请输入开票金额', trigger: 'blur' },
          ],
          receiver:[
            { required: true, message: '收件人不能为空', trigger: 'blur' },
          ],
          revAddress:[
            { required: true, message: '收货地址不能为空', trigger: 'blur' },
          ],
          tel:[
            { required: true, message: '电话不能为空', trigger: 'blur' },
            {pattern:/^1[3456789]\d{9}$/,message: '请输入正确的手机号格式', trigger:'blur'}
          ],
          invoiceType:[
            { required: true, message: '发票类型必选', trigger: 'change' },
          ]
        }
      }
    },
    methods: {
      jsmoney(id) {
        this.$http.get("/information/customerLogin/payList/" + id).then((res) => {
          if (res.data.success) {
            this.zje=res.data.result
          } else {
            this.$message.error(res.data.message);
          }
        })
      },
      saveInvoice(formName){
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if(this.form.amount<=this.zje){
              this.$http.post("/Invoice/pi-invoice/addInvoice",this.form).then((res) => {
                if (res.data.success) {
                  this.$message.success(res.data.message);
                  this.$router.go(0)
                } else {
                  this.$message.error(res.data.message);
                }
              })
            }else{
              this.$message.error("剩余额度不足！！！！");
            }
          } else {
            this.$message.error("关键信息不能为空");
            return false;
          }
        });
      },
      getcode(){
        this.$http.post("/order/addOrder/resultCode",{}).then((res) => {
          if(res.data.success){
            this.form.code=res.data.result
          }else{
            this.$message.error(res.data.message)
          }
        })
      }
    },
    mounted() {
      this.getcode()
      this.nowCustomer = JSON.parse(sessionStorage.getItem("customer"))
      let linkman = JSON.parse(sessionStorage.getItem("linkman"))
      this.id=this.nowCustomer.id
      let uu=this.nowCustomer
      this.form.invoiceTitle = uu.invoiceTitle
      this.form.invoiceType = uu.invoiceType
      this.form.invoiceNum = uu.invoiceNum
      this.form.taxRate = uu.taxRate
      this.form.customerId = uu.id
      this.form.customername = uu.name
      this.form.createUser = linkman.id
      this.form.receiver = linkman.name
      this.form.tel = linkman.tel
      this.form.revAddress = linkman.recvAddress
      this.jsmoney(this.form.customerId)
    }
  }
</script>

<style scoped>

</style>
